package cn.shop.admin.controller;

import cn.shop.admin.entity.backPermissionModule.BackPermission;
import cn.shop.admin.req.AssignPermissionReq;
import cn.shop.admin.service.BackPermissionService;
import cn.shop.base.MyPage;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.constant.Constant;
import cn.shop.utils.enums.OperationType;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 总后台 权限表(BackPermission)表控制层
 *
 * @author jzw
 * @since 2021-11-08 13:01:51
 */
@Validated
@RestController
@RequestMapping("/api/v1/backPermission")
public class BackPermissionController {

	/**
	 * 服务对象
	 */
	@Resource
	private BackPermissionService backPermissionService;

	/**
	 * 查询完整权限树
	 *
	 * @author jzw
	 * @since 2021/11/8 14:02
	 */
	@GetMapping("allMenuTree")
	public ApiResult<List<BackPermission>> allPermissionTree() {
		return ApiResult.success(backPermissionService.allPermissionTree());
	}

	@DeleteMapping("menuOrPermission")
	@OperationLog(module = "权限表模块", type = OperationType.UPDATE, desc = "停启用权限")
	public ApiResult<Map<String, Object>> delMenuOrPermission(Long id) {
		BackPermission permission = new BackPermission();
		LambdaUpdateWrapper<BackPermission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(BackPermission::getInitial, false).setSql("enable = not enable").eq(BackPermission::getId, id);
		return ApiResult.result(backPermissionService.update(permission, updateWrapper));
	}

	/**
	 * 分配权限
	 *
	 * @param assignPermissionReq 实体对象
	 * @return 新增结果
	 */
	@PostMapping("assignPermission")
	public ApiResult<Map<String, Object>> assignPermissions(@Validated @RequestBody AssignPermissionReq assignPermissionReq) {
		return ApiResult.result(backPermissionService.assignPermissions(assignPermissionReq));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page           分页对象
	 * @param backPermission 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BackPermission>> queryByPage(MyPage<BackPermission> page, BackPermission backPermission,
	                                                     @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                                     @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		return ApiResult.success(backPermissionService.page(page, Wrappers.lambdaQuery(backPermission)
				.apply(startTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<BackPermission> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(backPermissionService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param backPermission 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增权限", type = OperationType.CREATE)
	public ApiResult<Map<String, Object>> insert(@RequestBody BackPermission backPermission) {
		return ApiResult.result(backPermissionService.insert(backPermission));
	}

	/**
	 * 修改数据
	 *
	 * @param backPermission 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改权限", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@RequestBody BackPermission backPermission) {
		return ApiResult.result(backPermissionService.update(backPermission));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		BackPermission permission = new BackPermission();
		LambdaUpdateWrapper<BackPermission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(BackPermission::getId, id).eq(BackPermission::getInitial, false);
		return ApiResult.result(backPermissionService.deleteWithFill(permission, updateWrapper));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jzw
	 * @since 2021-11-08 13:01:51
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		BackPermission permission = new BackPermission();
		permission.setInitial(false);
		return ApiResult.result(backPermissionService.batchDelete(idList, permission));
	}

}

